package com.ysstech.finance.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysstech.common.entity.PageBean;
import com.ysstech.common.util.DateUtil;
import com.ysstech.finance.entity.*;
import com.ysstech.finance.mapper.SubjectProjectMapper;
import com.ysstech.finance.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-04-07
 */
@Service
public class SubjectProjectServiceImpl extends ServiceImpl<SubjectProjectMapper, SubjectProject> implements SubjectProjectService {

    /**
     * 查询核算方案列表信息
     *
     * @param subjectProject
     * @param pageBean
     * @return
     * @throws Exception
     */
    public PageBean<SubjectProject> queryAllSubjectProject(SubjectProject subjectProject, PageBean pageBean) throws Exception {
        QueryWrapper<SubjectProject> wrapper = this.getQueryWrapper(subjectProject);
        IPage pageAccountingProject = this.page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), wrapper);
        this.switchedListAccountingProject(pageAccountingProject.getRecords());
        return new PageBean<>(pageAccountingProject);
    }

    /**
     * 类型，机构等中文的转换
     *
     * @param list
     * @return
     * @throws Exception
     */
    public void switchedListAccountingProject(List<SubjectProject> list) throws Exception {
    }

    /**
     * 查询条件
     *
     * @param subjectProject
     * @return
     * @throws Exception
     */
    private QueryWrapper<SubjectProject> getQueryWrapper(SubjectProject subjectProject) throws Exception {
        QueryWrapper<SubjectProject> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("ADD_TIME");
        if (null == subjectProject) {
            return wrapper;
        }
        if (StringUtils.isNotBlank(subjectProject.getName())) {
            wrapper.like("NAME", subjectProject.getName());
        }
        return wrapper;
    }

    /**
     * 新准核算方案
     *
     * @throws Exception
     */
    public void addSubjectProject(SubjectProject subjectProject) throws Exception {
        String id = DateUtil.generateUUID();
        subjectProject.setId(id);
        subjectProject.setAddTime(LocalDateTime.now());
        this.save(subjectProject);
        // 创建对应方案的科目信息 将系统模板的方案复制一份

    }

    /**
     * 删除核算方案信息
     *
     * @param list
     */
    @Transactional(rollbackFor = Exception.class)
    public void delSubjectProject(List<String> list) throws Exception {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        this.removeByIds(list);
        //删除科目辅助表里的该方案的科目
    }

}
